#! /bin/bash
#PBS -N Test_GPU_Clust
#PBS -q class
#PBS -o output/test_mm_sf.out
#PBS -e output/test_mm_sf.err
#PBS -d /nethome/jchua3/vetter/cse6220/project/test/

cd $PBS_O_WORKDIR
echo "*****we are in "
echo $PBS_O_WORKDIR
echo "*****"

mpirun -n 1 -machinefile $PBS_NODEFILE R --vanilla > output/test_mm_sf_R.out <<EOF

library(snowfall)
library(gputools)
library(MASS) 

setwd("/nethome/jchua3/vetter/cse6220/project/test/output")

sfInit(parallel=TRUE, cpus=$np, type="MPI")


#prepare data
mu <- mat.or.vec(100,1)
cov <- mat.or.vec(100,100)
diag(cov) <- 1
A <- mvrnorm(100,mu,cov)
B <- mvrnorm(100,mu,cov)
C <- mvrnorm(100,mu,cov)
data <- list(A,B)

#run GPU matmult and save results
results <- sfClusterApplyLB(data, gpuMatMult, C)   
save(list=ls(all=TRUE), file="test_mm_results_sf.Rdata")
sfStop()

EOF



